var t = getApp();

Page({
    data: {
        canvasInfo: {
            id: "mycanvas",
            width: 320,
            height: 568
        },
        showInfo: {
            width: 320,
            height: 568
        },
        systemInfo: {
            width: 320,
            height: 568,
            screenHeight: 0
        },
        fixedHeight: 0,
        img: "",
        isChoose: !1,
        ad: {}
    },
    onShow(){
        this.getRandomAd()
        getApp().chaping()
    },
    onLoad: function(e) {
        var i = this;
        getApp().getRandomAd(true,this)
        this.getRandomAd(true)
        wx.getSystemInfo({
            success: function(t) {
                console.log(t.screenHeight), i.setData({
                    "systemInfo.width": t.windowWidth,
                    "systemInfo.height": t.windowHeight,
                    "systemInfo.screenHeight": t.screenHeight
                });
            }
        })
    },
    imageToCanvas: function(t) {
        var e = this, i = e.data.canvasInfo, s = wx.createCanvasContext(i.id, e);
        s.drawImage(t, 0, 0, i.width, i.height), s.draw();
    },
    cutImage: function(t) {
        var e = this;
        wx.getImageInfo({
            src: t,
            success: function(i) {
                var s = i.width, a = i.height;
                wx.getSystemInfo({
                    success: function(i) {
                        e.setData({
                            "systemInfo.width": i.windowWidth,
                            "systemInfo.height": i.windowHeight,
                            "canvasInfo.width": s,
                            "canvasInfo.height": a,
                            "systemInfo.screenHeight": i.screenHeight,
                            "showInfo.width": i.windowWidth,
                            "showInfo.height": i.windowWidth / s * a
                        });
                        var n = e.data.systemInfo.height / 2, o = e.data.showInfo.height + 10;
                        console.log("fh:" + n), console.log("ch:" + o), o > n && e.setData({
                            fixedHeight: o - n
                        }), e.imageToCanvas(t);
                    }
                });
            }
        });
    },
    previewImage: function(t) {
        var e = this, i = e.data.canvasInfo, s = t.currentTarget.dataset.index;
        wx.canvasToTempFilePath({
            canvasId: i.id,
            x: s % 3 * i.width / 3,
            y: parseInt(s / 3) * i.height / 3,
            width: i.width / 3,
            height: i.height / 3,
            success: function(t) {
                console.log("成功路径： " + t.tempFilePath);
                var e = [];
                e.push(t.tempFilePath), wx.previewImage({
                    urls: e
                });
            }
        }, e);
    },
    chooseImage: function(t) {
        var e = this;
        wx.chooseImage({
            count: 1,
            sizeType: [ "compressed" ],
            success: async function(t) {
                let imgData = await getApp().checkImg(t.tempFilePaths[0])
                console.log(imgData)
                if(!imgData||imgData.result.errCode!==0){
                    wx.showModal({
                    confirmText: '确定',
                    content: '图片内容不合法，请重新选择',
                    showCancel: false
                    })
                    return
                }
                e.setData({
                    img: t.tempFilePaths[0],
                    isChoose: !0
                }), e.cutImage(t.tempFilePaths[0]);
            }
        });
    },
    onShareAppMessage: function(t) {
        return {
            title: "九宫格切图-图片编辑助手",
            path: "/pages/cuttingNine/cuttingNine"
        };
    },
    
    saveAll: function() {
        this.getRandomAd()
        var t = this, e = t.data.canvasInfo;
        wx.showLoading({
            title: "正在保存"
        });
        for (var i = 0; i < 9; i++) {
          console.log(i % 3 * e.width / 3, parseInt(i / 3) * e.height / 3)
          wx.canvasToTempFilePath({
            canvasId: e.id,
            x: i % 3 * e.width / 3,
            y: parseInt(i / 3) * e.height / 3,
            width: e.width / 3,
            height: e.height / 3,
            success: function (t) {
              wx.saveImageToPhotosAlbum({
                filePath: t.tempFilePath,
                success: function (t) {
                  wx.hideLoading({
                    complete: function (t) {
                      wx.showToast({
                        title: "保存成功"
                      });
                    }
                  });
                },
                fail: function (t) {
                  wx.hideLoading()
                  wx.showModal({
                    title: '提示',
                    content: '需要您授权保存相册',
                    showCancel: false,
                    success: modalSuccess => {
                      wx.openSetting({
                        success(settingdata) {
                          console.log("settingdata", settingdata)
                          if (settingdata.authSetting['scope.writePhotosAlbum']) {
                            wx.showModal({
                              title: '提示',
                              content: '获取权限成功,再次点击即可保存。',
                              showCancel: false,
                            })
                          } else {
                            wx.showModal({
                              title: '提示',
                              content: '获取权限失败，将无法保存到相册哦~',
                              showCancel: false,
                            })
                          }
                        },
                        fail(failData) {
                          console.log("failData", failData)
                        },
                        complete(finishData) {
                          console.log("finishData", finishData)
                        }
                      })
                    }
                  })
                }
              });
            }
          }, t);
        }
    }
});